<template>
  <div class="container">
    <div class="background-image"></div>
    <div class="content">
      <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="rules" @submit.native.prevent="handleLogin">
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
          </el-form-item>
          <router-link to="/register" class="register-link">没有账号？马上注册！</router-link>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PetLogin',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '账号长度必须在3到20个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度必须在6到20个字符之间', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    async submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            const response = await this.$axios.post('/api/auth/login', this.loginForm);
            localStorage.setItem('token', response.data.token);
            localStorage.setItem('isLoggedIn', 'true');
            this.$axios.defaults.headers.common['Authorization'] = `Bearer ${response.data.token}`;
            this.$emit('update:isLoggedIn', true);
            this.$router.push({ name: 'create-profile' }); // 确保重定向到正确的路由
          } catch (error) {
            this.$message.error('登录失败，请检查用户名和密码');
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.container {
  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background-color: #000; /* 备用背景颜色 */
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/background.jpg'); /* 使用相对路径引入图片 */
  background-size: cover;
  background-position: center;
  opacity: 0.8; /* 根据需要调整透明度 */
}

.content {
  position: relative;
  z-index: 1;
  width: 300px;
  padding: 20px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
  background: rgba(255, 255, 255, 0.9); /* 设置背景颜色和透明度 */
  border-radius: 8px; /* 设置圆角 */
}

.login-container {
  display: flex;
  flex-direction: column;
  align-items: center; /* 居中所有子元素 */
  width: 100%;
}

.el-form-item {
  width: 100%; /* 确保表单项宽度与容器一致 */
}

.register-link {
  display: block;
  margin-top: 10px;
  color: #42b983;
}
</style>
